<?xml version="1.0" encoding="UTF-8" ?>
<!--

    Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
    under one or more contributor license agreements. See the NOTICE file
    distributed with this work for additional information regarding copyright
    ownership. Camunda licenses this file to you under the Apache License,
    Version 2.0; you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 

<mapper namespace="org.camunda.bpm.engine.impl.persistence.entity.JobDefinitionEntity">

  <!-- JOB DEFINITION INSERT -->
  <insert id="insertJobDefinition" parameterType="org.camunda.bpm.engine.impl.persistence.entity.JobDefinitionEntity">
    insert into ${prefix}ACT_RU_JOBDEF (
      ID_,
      PROC_DEF_ID_,
      PROC_DEF_KEY_,
      ACT_ID_,
      JOB_TYPE_,
      JOB_CONFIGURATION_,
      JOB_PRIORITY_,
      SUSPENSION_STATE_,
      TENANT_ID_,
      DEPLOYMENT_ID_,
      REV_
      )
    values (
      #{id ,jdbcType=VARCHAR},
      #{processDefinitionId, jdbcType=VARCHAR},
      #{processDefinitionKey, jdbcType=VARCHAR},
      #{activityId, jdbcType=VARCHAR},
      #{jobType, jdbcType=VARCHAR},
      #{jobConfiguration, jdbcType=VARCHAR},
      #{jobPriority, jdbcType=BIGINT},
      #{suspensionState, jdbcType=INTEGER},
      #{tenantId, jdbcType=VARCHAR},
      #{deploymentId, jdbcType=VARCHAR},
      1
    )
  </insert>

  <!-- JOB DEFINITION UPDATE -->

  <update id="updateJobDefinition" parameterType="org.camunda.bpm.engine.impl.persistence.entity.JobDefinitionEntity">
    update ${prefix}ACT_RU_JOBDEF set
      REV_ = #{revisionNext, jdbcType=INTEGER},
      PROC_DEF_ID_ = #{processDefinitionId, jdbcType=VARCHAR},
      PROC_DEF_KEY_ = #{processDefinitionKey, jdbcType=VARCHAR},
      ACT_ID_ = #{activityId, jdbcType=VARCHAR},
      JOB_TYPE_ = #{jobType, jdbcType=VARCHAR},
      JOB_CONFIGURATION_ = #{jobConfiguration, jdbcType=VARCHAR},
      JOB_PRIORITY_ = #{jobPriority, jdbcType=BIGINT},
      SUSPENSION_STATE_ = #{suspensionState, jdbcType=INTEGER},
      DEPLOYMENT_ID_ = #{deploymentId, jdbcType=VARCHAR}
    where ID_ = #{id, jdbcType=VARCHAR}
      and REV_ = #{revision, jdbcType=INTEGER}
  </update>

  <update id="updateJobDefinitionSuspensionStateByParameters" parameterType="org.camunda.bpm.engine.impl.db.ListQueryParameterObject">
    update ${prefix}ACT_RU_JOBDEF set
      REV_ = 1 + REV_ ,
      SUSPENSION_STATE_ = #{parameter.suspensionState, jdbcType=INTEGER}
    <where>
      <if test="parameter.jobDefinitionId != null">
        ID_ = #{parameter.jobDefinitionId, jdbcType=VARCHAR}
      </if>
      <if test="parameter.processDefinitionId != null">
        and PROC_DEF_ID_ = #{parameter.processDefinitionId, jdbcType=VARCHAR}
      </if>
      <if test="parameter.processDefinitionKey != null">
        <if test="!parameter.isProcessDefinitionTenantIdSet">
          and PROC_DEF_KEY_ = #{parameter.processDefinitionKey, jdbcType=VARCHAR}
        </if>
        <if test="parameter.isProcessDefinitionTenantIdSet">
          and PROC_DEF_ID_ IN (
            SELECT ID_ 
            FROM ${prefix}ACT_RE_PROCDEF PD
            WHERE PD.KEY_ = #{parameter.processDefinitionKey, jdbcType=VARCHAR}
            <if test="parameter.processDefinitionTenantId != null">
              and PD.TENANT_ID_ = #{parameter.processDefinitionTenantId, jdbcType=VARCHAR}
            </if>
            <if test="parameter.processDefinitionTenantId == null">
              and PD.TENANT_ID_ is null
            </if>
          )
        </if>
        <bind name="columnPrefix" value="''"/>
        <include refid="org.camunda.bpm.engine.impl.persistence.entity.TenantEntity.queryTenantCheckWithPrefix" />
      </if>
    </where>
  </update>

  <!-- JOB DEFINITION DELETE -->

  <delete id="deleteJobDefinition" parameterType="org.camunda.bpm.engine.impl.persistence.entity.JobDefinitionEntity">
    DELETE FROM
        ${prefix}ACT_RU_JOBDEF
    where
        ID_ = #{id}
        and REV_ = #{revision}
  </delete>

  <delete id="deleteJobDefinitionsByProcessDefinitionId" parameterType="string">
    DELETE FROM
        ${prefix}ACT_RU_JOBDEF
    where
        PROC_DEF_ID_ = #{id}
  </delete>

  <!-- JOB DEFINITION RESULTMAP -->

  <resultMap id="jobDefinitionResultMap" type="org.camunda.bpm.engine.impl.persistence.entity.JobDefinitionEntity">
    <id property="id" column="ID_" jdbcType="VARCHAR" />
    <result property="revision" column="REV_" jdbcType="INTEGER" />
    <result property="processDefinitionId" column="PROC_DEF_ID_" jdbcType="VARCHAR" />
    <result property="processDefinitionKey" column="PROC_DEF_KEY_" jdbcType="VARCHAR" />
    <result property="activityId" column="ACT_ID_" jdbcType="VARCHAR" />
    <result property="jobType" column="JOB_TYPE_" jdbcType="VARCHAR" />
    <result property="jobConfiguration" column="JOB_CONFIGURATION_" jdbcType="VARCHAR" />
    <result property="jobPriority" column="JOB_PRIORITY_" jdbcType="BIGINT" />
    <result property="suspensionState" column="SUSPENSION_STATE_" jdbcType="INTEGER" />
    <result property="tenantId" column="TENANT_ID_" jdbcType="VARCHAR" />
    <result property="deploymentId" column="DEPLOYMENT_ID_" jdbcType="VARCHAR" />
  </resultMap>

  <!-- SELECTS -->

  <select id="selectJobDefinition" parameterType="string" resultMap="jobDefinitionResultMap">
    select * from ${prefix}ACT_RU_JOBDEF where ID_ = #{id}
  </select>

  <select id="selectJobDefinitionsByProcessDefinitionId" resultMap="jobDefinitionResultMap">
    select * from ${prefix}ACT_RU_JOBDEF where PROC_DEF_ID_ = #{parameter}
  </select>

  <select id="selectJobDefinitionByQueryCriteria" parameterType="org.camunda.bpm.engine.impl.JobDefinitionQueryImpl" resultMap="jobDefinitionResultMap">
    <include refid="org.camunda.bpm.engine.impl.persistence.entity.Commons.bindOrderBy"/>
    ${limitBefore}
    select ${distinct} RES.*
    ${limitBetween}
    <include refid="selectJobDefinitionByQueryCriteriaSql"/>
    ${orderBy}
    ${limitAfter}
  </select>

  <select id="selectJobDefinitionCountByQueryCriteria" parameterType="org.camunda.bpm.engine.impl.JobDefinitionQueryImpl" resultType="long">
    ${countDistinctBeforeStart} RES.ID_ ${countDistinctBeforeEnd}
    <include refid="selectJobDefinitionByQueryCriteriaSql"/>
    ${countDistinctAfterEnd}
  </select>

  <sql id="selectJobDefinitionByQueryCriteriaSql">
    from ${prefix}ACT_RU_JOBDEF RES
    
    <if test="authCheck.shouldPerformAuthorizatioCheck &amp;&amp; authCheck.authUserId != null">
      left join ${prefix}ACT_RE_PROCDEF PROCDEF
      on RES.PROC_DEF_ID_ = PROCDEF.ID_           
      <if test="!authCheck.revokeAuthorizationCheckEnabled">    
        <include refid="org.camunda.bpm.engine.impl.persistence.entity.AuthorizationEntity.authCheckJoinWithoutOnClause" /> 
        AUTH ON (AUTH.RESOURCE_ID_ ${authJoinStart} RES.ID_ ${authJoinSeparator} PROCDEF.KEY_ ${authJoinSeparator} '*' ${authJoinEnd})      
      </if>
    </if>
    
    <where>
      <if test="id != null">
        RES.ID_ = #{id}
      </if>
      <if test="activityIds != null &amp;&amp; activityIds.length > 0">
        and RES.ACT_ID_ in
        <foreach item="item" index="index" collection="activityIds"
                 open="(" separator="," close=")">
          #{item}
        </foreach>
      </if>
      <if test="processDefinitionId != null">
        and RES.PROC_DEF_ID_ = #{processDefinitionId}
      </if>
      <if test="processDefinitionKey != null">
        and RES.PROC_DEF_KEY_ = #{processDefinitionKey}
      </if>
      <if test="jobType != null">
        and RES.JOB_TYPE_ = #{jobType}
      </if>
      <if test="jobConfiguration != null">
        and RES.JOB_CONFIGURATION_ = #{jobConfiguration}
      </if>
      <if test="suspensionState != null">
        and RES.SUSPENSION_STATE_ = #{suspensionState.stateCode}
      </if>
      <if test="withOverridingJobPriority != null &amp;&amp; withOverridingJobPriority">
        and RES.JOB_PRIORITY_ is not null
      </if>
      <if test="isTenantIdSet">
        <if test="tenantIds != null &amp;&amp; tenantIds.length > 0">
          and ( RES.TENANT_ID_ in
          <foreach item="tenantId" index="index" collection="tenantIds"
                   open="(" separator="," close=")">
            #{tenantId}
          </foreach>
          <if test="includeJobDefinitionsWithoutTenantId">
            or RES.TENANT_ID_ is null 
          </if>
          )
        </if>
        <if test="tenantIds == null">
          and RES.TENANT_ID_ is null
        </if>
      </if>

      <include refid="org.camunda.bpm.engine.impl.persistence.entity.AuthorizationEntity.queryAuthorizationCheck" />
      <include refid="org.camunda.bpm.engine.impl.persistence.entity.TenantEntity.queryTenantCheck" />

    </where>
  </sql>

</mapper>
